module WorksHelper
  def add_picture(form_builder)
    link_to_function "上传作品", :id  => "add_picture" do |page|
      form_builder.fields_for :pictures, Picture.new, :child_index => 'NEW_RECORD' do |picture_form|
        html = render(:partial => 'picture', :locals => { :f => picture_form })
        page << "$('#{escape_javascript(html)}'.replace(/NEW_RECORD/g, new Date().getTime())).insertBefore('#add_picture');"
      end
    end
  end
  
  def delete_picture(form_builder)
    if form_builder.object.new_record?
      link_to_function("删除", "$(this).parent('div.picture').remove()")
    else
      form_builder.hidden_field(:_destroy) +
      link_to_function("删除", "$(this).parent('div.picture').hide(); $(this).prev().val('1')")
    end
  end
end
